<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <title>消息列表--layui后台管理模板</title>
    <link rel="stylesheet" type="text/css" th:href="@{/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/news.css}" media="all"/>
</head>
<body class="childrenBody">
<blockquote class="layui-elem-quote news_search">
    <form id="searchForm" name="searchForm" class="layui-form" action="/message/message.html">
        <input type="hidden" id="pageNum" name="pageNum" th:value="${page.pageNum}"/>
        <div class="layui-form-item" style="margin-left: -43px;margin-bottom: 2px;">
            <label class="layui-form-label">标题:</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="title" th:value="${title}"/>
            </div>
            <label class="layui-form-label">类型:</label>
            <div class="layui-input-inline">
                <select name="type" id="type">
                    <option value="-1">请选择消息类型</option>
                    <option value="0" th:selected="${type==0}">黄金资讯</option>
                    <option value="1" th:selected="${type==1}">系统公告</option>
                    <option value="2" th:selected="${type==2}">营销活动</option>
                    <!--   <option value="3"  th:selected="${type==3}">金豆收取</option> -->
                </select>
            </div>
            <button class="layui-btn search_btn"><i class="layui-icon">&#xe615;</i>查询</button>
            <a class="layui-btn layui-btn-normal newsAdd_btn" id="add"><i class="layui-icon">&#xe608;</i>添加</a>
        </div>
    </form>
</blockquote>
<div class="layui-form news_list" style="min-width: 1680px;">
    <table class="layui-table">
        <thead>
        <tr>
            <th>标题</th>
            <th>类型</th>
            <th>目标用户</th>
            <th>活动地址</th>
            <th>状态</th>
            <th>创建时间</th>
            <th>更新时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody class="news_content">
        <th:block th:if="${not #lists.isEmpty(page.list)}">
            <tr th:each="item:${page.list}">
                <td th:text="${item.title}"></td>
                <th:block th:switch="${item.type}">
                    <td th:case="0">黄金资讯</td>
                    <td th:case="1">系统公告</td>
                    <td th:case="2">营销活动</td>
                    <td th:case="3">金豆收取</td>
                </th:block>
                <td th:text="${item.userId}?${item.userId}"></td>
                <td th:text="${item.hrefUrl} ? ${item.hrefUrl} : '暂未设置' "></td>
                <th:block th:switch="${item.status}">
                    <td th:case="0">未推送</td>
                    <td th:case="1">已推送</td>
                </th:block>
                <td th:text="${#dates.format(item.createTime,'yyyy-MM-dd HH:mm:ss')}"></td>
                <td th:text="${item.updateTime}?${#dates.format(item.updateTime,'yyyy-MM-dd HH:mm:ss')}"></td>
                <td>
                    <a th:data="${item.id}" class="layui-btn layui-btn-mini message_edit"> <i class="layui-icon">&#xe642;</i>编辑</a>
                    <th:block th:if="${item.status == 0}">
                        <a th:data="${item.id}" class="layui-btn layui-btn-danger layui-btn-mini message_push"> <i
                                class="layui-icon">&#xe642;</i>推送</a>
                     <a th:data="${item.id}" class="layui-btn layui-btn-danger layui-btn-mini message_del" ><i class="layui-icon">&#xe640;</i> 删除</a>
                    </th:block>
                </td>
            </tr>
        </th:block>
        </tbody>
    </table>
</div>
<div id="paged" align="right"></div>
<script type="text/javascript" th:src="@{/layui/layui.js}"></script>
<script type="text/javascript" th:src="@{/jquery/jquery-3.2.1.min.js}"></script>
</body>
<script th:inline="javascript">
    layui.use('laypage', function () {
        var laypage = layui.laypage;
        //执行一个laypage实例
        laypage.render({
            elem: 'paged', //注意，这里的 test1 是 ID，不用加 # 号
            curr: [[${page.pageNum}]],
            count: [[${page.total}]], //数据总数，从服务端得到
            layout: ['count', 'prev', 'page', 'next'],
            jump: function (obj, first) {
                //得到了当前页，用于向服务端请求对应数据
                var curr = obj.curr;
                $("#pageNum").val(curr);
                if (!first) {
                    $("#searchForm").submit();
                }
            }
        });
    });

    layui.use('layer', function () {
        var layer = layui.layer, $ = layui.jquery;

        //删除
        $(".message_del").bind("click", function () {
            var id = $(this).attr("data");
            layer.confirm('确定要删除吗?', {
                    icon: 3,
                    skin: 'layui-layer-lan',
                    closeBtn: 0
                },
                function () {
                    $.get("toDelete.html?id=" + id, function (datas) {
                        console.log(datas);
                        var result = JSON.parse(datas);
                        if (result.code > 0) {
                            layer.msg(result.msg, {
                                time: 2000, //3s后自动关闭
                            });
                            setTimeout(function () {
                                location.reload(true);
                            }, 500);
                        } else {
                            layer.msg(result.msg, {
                                time: 1000, //1s后自动关闭
                            });
                            setTimeout(function () {
                                location.reload(true);
                            }, 500)
                        }
                    })
                }, function () {
                });
        });


        //编辑
        $(".message_edit").bind("click", function () {
            var id = $(this).attr("data");
            console.log(id);
            //Ajax获取
            $.post('toEdit.html', {"id": id}, function (data) {
                layer.open({
                    type: 1,
                    title: "编辑消息"
                    , area: ['900px', '460px']
                    , shade: 0
                    , content: data
                    , yes: function (index, layero) {
                        layer.close(index);
                    }
                });
            });
        });

        //推送
        $(".message_push").bind("click", function () {
            var id = $(this).attr("data");
            layer.confirm('确认推送吗?', {icon: 3, title: '提醒'}, function (index) {
                //Ajax获取
                $.post('pushMessage', {id: id}, function (data) {
                    if (data) {
                        $("#searchForm").submit();
                        layer.close(index);
                    } else {
                        layer.msg("推送失败请稍后再试");
                    }
                });
            });
        });

        //新增
        $("#add").bind("click", function () {
            $.get('toInsert.html', function (data) {
                layer.open({
                    type: 1,
                    title: "添加消息"
                    , area: ['680px', '450px']
                    , shade: 0
                    , content: data
                    , yes: function (index, layero) {
                        layer.close(index);
                    }
                });
            });
        });

        function form_html(id, title) {
            $.post('messageAdd.html', {id: id}, function (data) {
                layer.open({
                    type: 1,
                    title: title
                    , area: ['680px', '450px']
                    , shade: 0
                    , content: data
                    , yes: function (index, layero) {
                        layer.close(index);
                    }
                });
            });
        };
    });

    layui.use('form', function () {
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功

        //……

        //但是，如果你的HTML是动态生成的，自动渲染就会失效
        //因此你需要在相应的地方，执行下述方法来手动渲染，跟这类似的还有 element.init();
        // form.render();
    });
</script>
</html>